<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     * 列表页
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = User::orderBy("id","desc")->where(function($query) use($request){
            $username =$request->input('username');
            $email =$request->input('email');
            if($username){
                $query->where('username','like','%'.$username.'%');
            }
            if($email){
                $query->where('username','like','%'.$email.'%');
            }
        })->paginate($request->input("num")?$request->input("num"):10);
        return view("admin.user.list",compact("user","request"));
    }

    /**
     * Show the form for creating a new resource.
     * 用户添加页面
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.user.add");
    }

    /**
     * Store a newly created resource in storage.
     *添加操作
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user = $request->post();
        $user['username'] = htmlspecialchars($user['username']);
        $user['password'] = Hash::make($user['password']);

        $res = User::create($user);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    /**
     * Display the specified resource.
     *显示一条用户数据
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *返回修改页面
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::find($id);
        return view("admin.user.edit",compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *执行修改操作
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $user = $request -> all();
        if($request->input('password')){
            $user['password'] = Hash::make($request->input('password'));
        }
        if(isset($user['is_admin'])){
            $user['is_admin'] = 1 ;
        }
        $res = User::where("id",$id)->update($user);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }


    /**
     * Remove the specified resource from storage.
     *删除操作
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request,$id)
    {
        $user = User::find($id);
        $res = $user->delete();
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }

    public function delAll(Request $request){
        $res = User::destroy($request->input('ids'));
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }
}
